using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using TEngine;
using System;
using static GameLogic.SocketDefinition;

namespace GameLogic
{
    [Window(UILayer.UI)]
    class LoginUI : UIWindow
    {
        #region 脚本工具生成的代码
        private InputField _inputAccount;
        private InputField _inputPassword;
        private Button _btnLogin;
        protected override void ScriptGenerator()
        {
            _inputAccount = FindChildComponent<InputField>("m_inputAccount");
            _inputPassword = FindChildComponent<InputField>("m_inputPassword");
            _btnLogin = FindChildComponent<Button>("m_btnLogin");
            _btnLogin.onClick.AddListener(OnClickLoginBtn);
        }
        #endregion

        #region 事件
        private void OnClickLoginBtn()
        {
            // 获取输入框内容
            string account = _inputAccount.text;
            string password = _inputPassword.text;

            // 输入验证
            if (string.IsNullOrEmpty(account))
            {
                Log.Error("账号不能为空");
                return;
            }
            if (string.IsNullOrEmpty(password))
            {
                Log.Error("密码不能为空");
                return;
            }

            try
            {
                 Login(account, password);
            }
            catch (Exception e)
            {
                Log.Error($"登录失败: {e.Message}");
            }
        }

        private void Login(string account, string password)
        {
            SocketProxy.SocketLogin(accountType:LOGIN_TYPE.USER_NAME, loginID: account, password:password, market:"", appStr: "NBDT_PC");
        }

        #endregion
        protected override void RegisterEvent()
        {
            //AddUIEvent(ILoginUI_Event.ShowLoginUI, () =>
            //{
            //    Log.Debug("ILoginUI_Event.ShowLoginUI 事件触发");
            //});
        }
    }
}

